package com.task.mall.controller.admin;

import com.google.gson.Gson;
import com.task.mall.bean.Result;
import com.task.mall.bean.Type;
import com.task.mall.service.TypeService;
import com.task.mall.service.impl.TypeServiceImpl;
import com.task.mall.utils.HttpUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/api/admin/type/*")
public class TypeServlet extends HttpServlet {

    private TypeService typeService = new TypeServiceImpl();
    Gson gson = new Gson();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/admin/type/", "");
        if("addType".equals(action)){
            addType(request,response);
        }
    }

    /**
     * 增加具体的商品类目（大类）
     * @param request
     * @param response
     */
    private void addType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        Type type = gson.fromJson(requestBody, Type.class);
        int result = typeService.addType(type);
        Result res = new Result();
        if (result == 200){
            res.setCode(0);
        }else {
            res.setCode(10000);
            res.setData("添加商品类目异常，请稍后重试");
        }
        response.getWriter().println(gson.toJson(res));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/admin/type/", "");
        if ("getType".equals(action)){
            getType(request,response);
        }if ("deleteType".equals(action)){
            deleteType(request,response);
        }
    }

    /**
     * 删除条目
     * @param request
     * @param response
     */
    private void deleteType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int typeId = Integer.parseInt(request.getParameter("typeId"));
        int result = typeService.deleteType(typeId);
        Result res = new Result();
        if (result == 200){
            res.setCode(0);
        }else {
            res.setCode(10000);
            res.setMessage("删除类目异常，请稍后重试");
        }
        response.getWriter().println(gson.toJson(res));
    }

    /**
     * 获取具体的Type类目
     * @param request
     * @param response
     */
    private void getType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Type> typeList = typeService.getType();
        Result res = new Result();
        res.setCode(0);
        res.setData(typeList);
        response.getWriter().println(gson.toJson(res));
    }
}
